<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
	<title>社医堂</title>
	<link rel="stylesheet" type="text/css" href="../../../css/api.css" />
	<link rel="stylesheet" type="text/css" href="../../../css/hui.css" />
	<link rel="stylesheet" type="text/css" href="../../../css/iconfont.css" />
</head>

<body>
	<style>
		table tr td {
			padding:5px;
		}

		#num {
			width: 100px;
			border: solid 1px #ccc;
			text-align: center;
			font-size: 25px;
		}

		body {
			background-color: #fff;
		}
		table tr td{padding:5px;}
	</style>

	<div class="hui-wrap">


		<div style="padding:15px;border-radius:6px;">

			<table  style="width:90%;">
				<tbody id="cont">
					<tr>
						<td>
							<div class="hui-form-select" id="category"></div>
						</td>
						<td>
							<div class="hui-form-select" id="druglist">
								<select name="drug" style="float:right;width:130px;"></select>
							</div>
						</td>
						<td><input type="number" class="hui-input hui-input-clear" id="num" value="1" placeholder="药品数量" style="width:40px;padding:5px;" /></td>
						<td><button type="button" onclick="copy()" class="hui-button hui-fl hui-button-small">增加</button></td>
					</tr>
				</tbody>
				<tbody id="newlist" style="padding:5px;"></tbody>
				
			</table>
		</div>
		<button type="button" onclick="drugpost()" class="hui-button hui-button-large hui-primary" style="margin-top:15px;margin-left:15%;width:70%;">提交数据</button>


	</div>


	<script src="../../../script/hui.js" type="text/javascript" charset="utf-8"></script>
	<script src="../../../script/hui-tab.js" type="text/javascript"></script>
	<script src="../../../script/api.js" type="text/javascript" charset="utf-8"></script>
	<script src="../../../script/stm.js" type="text/javascript" charset="utf-8"></script>
	<script type="text/javascript">
		apiready = function () {

			localStorage.clear();

			api.ajax({
				url: $api.getStorage('geturl')+'&c=ajaxmember&a=drugcategory',
				method: 'post',
				data: {
					values: { 
						userid : $api.getStorage('userid'),
						docid  : $api.getStorage('docid'),
						hospid : $api.getStorage('hospid'),
					}
				}
			},function(ret, err){
				if (ret) {

					var html = '<select id="catid" onchange="selproduct(this.value)" style="float:right;width:80px;"><option value="0">请选择</option>';
					for(i=0;i<ret.data.length;i++){
						html += '<option value="'+ret.data[i].id+'='+ret.data[i].catname+'">'+ret.data[i].catname+'</option>';
					}
					html += '</select>';
					hui("#category").html(html);

				} else {
					alert( JSON.stringify( err ) );
				}

			});


		}




		function selproduct(id) {

			api.ajax({
				url: $api.getStorage('geturl')+'&c=ajaxmember&a=ajaxgetdrug',
				method: 'post',
				data: {
					values: { 
						userid : $api.getStorage('userid'),
						docid  : $api.getStorage('docid'),
						hospid : $api.getStorage('hospid'),
						catid  : id,
					}
				}
			},function(ret, err){
				if (ret) {
					
					var html = '<select class="drugnum" id="drug" style="float:right;width:130px;">';
					for(i=0;i<ret.data.length;i++){
						html += '<option value="'+ret.data[i].id+'='+ret.data[i].name+'">'+ret.data[i].name+'</option>';
					}
					html += '</select>';
					hui("#druglist").html(html);

				} else {
					alert( JSON.stringify( err ) );
				}

			});


		}

		
		function copy(){
			
			var index = localStorage.length
			
			var category = hui("#catid").val();
			var drug     = hui("#drug").val();
			var num      = hui("#num").val();
			var categoryArr = category.split("=")
			var drugArr     = drug.split("=")

			//检查有没有重复的
			for(i=0;i<index;i++){
				var list = localStorage.getItem(i);
				list = JSON.parse(list)
				if(list.drugname==drugArr[1]){
					api.toast({msg:'已经存在了', location: 'top'})
					return false;
				}
			}
			$api.setStorage('index',index+1)

			var key = index;
			var data = {};
			//data.catid   = categoryArr[0];
			//data.catname = categoryArr[1];
			data.drugid  = drugArr[0];
			data.drugname= drugArr[1];
			data.num     = num;
			localStorage.setItem(key, JSON.stringify(data));
			

			var html = '<tr id="list_'+index+'"><td>'+categoryArr[1]+'</td>';
				html += '<td>'+drugArr[1]+'</td>';
				html += '<td>'+num+'</td>';
				html += '<td><button type="button" onclick="del('+index+')" class="hui-button hui-fl hui-button-small">删除</button></td></tr>';
			
			$api.append($api.byId("newlist"),html);
		}

		function del(id){
			localStorage.removeItem(id);
			document.getElementById("list_"+id).remove()
		}


		function drugpost() {
			
			var index = localStorage.length
			var drugname='';
			var drugid  ='';
			var num     ='';

			for(i=0;i<index;i++){
				var list = localStorage.getItem(i);
				list = JSON.parse(list)
				if(list.drugname!='' && list.drugname!=undefined){
					drugname += list.drugname+","
					drugid   += list.drugid+","
					num      += list.num+","
				}				
			}
			drugpost_sub(drugid,drugname,num)
		}


		function drugpost_sub(drugid,drugname,num) {
			
			api.ajax({
				url: $api.getStorage('geturl')+'&c=ajaxmember&a=pharmacysave',
				method: 'post',
				data: {
					values: { 
						userid : $api.getStorage('userid'),
						docid  : $api.getStorage('docid'),
						hospid : $api.getStorage('hospid'),
						province : $api.getStorage('province'),
						city   : $api.getStorage('city'),
						area   : $api.getStorage('area'),
						drugid : drugid,
						drugname : drugname,
						num      : num,
					}
				}
			},function(ret, err){
				if (ret) {
					hui.iconToast(ret.tip, 'success');
					setTimeout("tolist()",1300)
				} else {
					alert("取药上报数据出错");
				}

			});


		}

		function tolist(){
			toDetails('./','up_drug_record',0,'')
		}
	</script>

</body>

</html>